C#非常强大的资源管理器(源码),用户自定义控件实现
using System;using System.Diagnostics;using System.Drawing;using System.IO;using System.Runtime.InteropServices;using System.Text;using System.Windows.Forms;namespace WindowsControl{ public partial class ExplorerControl : UserControl { #region API函数 [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi)] protected static extern int mciSendString(string lpstrCommand, StringBuilder lpstrReturnString, int uReturnLength, IntPtr hwndCallback); #endregion #region 公共变量 /// <summary> /// 本地当前路径 /// </summary> public string FilePathStr = ""; /// <summary> /// 所有磁盘信息 /// </summary> public DriveInfo[] Drivers = DriveInfo.GetDrives(); /// <summary> /// 服务器当前选中的路径 /// </summary> public string SitPath = "/"; /// <summary> /// 服务器名或IP地址 /// </summary> public string ServerName = "127.0.0.1"; /// <summary> /// 登陆服务器用户名 /// </summary> public string ServerUserName = ""; /// <summary> /// 登陆服务器密码 /// </summary> public string ServerPasswordName = "127.0.0.1"; /// <summary> /// 登陆服务器端口 /// </summary> public int ServerPort = 21; #endregion #region 资源管理器构造函数 public ExplorerControl() { InitializeComponent(); CreatTreeview(); } #endregion #region 树目录和listview更新方法 /// <summary> /// 初始化树目录 /// </summary> #region public void CreatTreeview() { //禁止任何树视图重绘 ExplorerTree.BeginUpdate(); //删除所有ExplorerTree中节点 ExplorerTree.Nodes.Clear(); TreeNode rootNode = new TreeNode(); //"我的电脑", 0, 0 rootNode.Text = string.Format("我的电脑"); rootNode.ImageIndex = 2; rootNode.SelectedImageIndex = 2; ExplorerTree.Nodes.Add(rootNode); //返回当前计算机中逻辑驱动器名称的数组 // #region//遍历驱动器名称,重画TreeView,按照windows习惯显示 //但是要用tn.Tag属性记录其绝对路径,以便后面操作(遍历文件夹下文件) int j = 0; foreach (DriveInfo driver in Drivers) { TreeNode tn = new TreeNode(driver.Name); tn.Tag = driver.RootDirectory.FullName; ExplorerTree.Nodes[0].Nodes.Add(tn); Icon fileIcon = GetSystemIcon.GetIcon(driver.Name, false); TVimageList.Images.Add(driver.Name, fileIcon); tn.ImageKey = driver.Name; tn.SelectedImageKey = driver.Name; AddDirectories(tn); if (driver.DriveType == DriveType.CDRom) { ExplorerTree.Nodes[0].Nodes[j].Text = string.Format("DVD RW 驱动器(" driver.Name.Remove(2) ")"); } else if (driver.DriveType == DriveType.Removable) { ExplorerTree.Nodes[0].Nodes[j].Text = string.Format("可移动磁盘(" driver.Name.Remove(2) ")"); } else if (driver.DriveType == DriveType.Fixed) { ExplorerTree.Nodes[0].Nodes[j].Text = string.Format("本地磁盘(" driver.Name.Remove(2) ")"); } else { ExplorerTree.Nodes[0].Nodes[j].Text = string.Format("UnknowDriver(" driver.Name.Remove(2) ")"); } j ; } #endregion // ExplorerTree.SelectedNode = rootNode.FirstNode; //启用视图重绘 ExplorerTree.EndUpdate(); } #endregion /// <summary> /// 在ListView中列出各个磁盘详细信息 /// </summary> #region private void InitDisksList() { listView磁盘文件.BeginUpdate(); listView磁盘文件.Clear(); #region//设置关于磁盘的ListView标题块 //设置默认显示方式 //listView磁盘文件.View = View.Tile; listView磁盘文件.Columns.Add("名称", 160, HorizontalAlignment.Left); listView磁盘文件.Columns.Add("类型", 120, HorizontalAlignment.Left); listView磁盘文件.Columns.Add("总大小", 120, HorizontalAlignment.Left); listView磁盘文件.Columns.Add("剩余空间", 200, HorizontalAlignment.Left); #endregion #region //向ListView中添加磁盘信息 foreach (DriveInfo Driver in Drivers) { ListViewItem fileItem = listView磁盘文件.Items.Add("本地磁盘(" Driver.Name.Remove(2) ")"); Icon fileIcon = GetSystemIcon.GetIcon(Driver.Name, false); this.TVimageList.Images.Add(fileItem.Text, fileIcon); this.LVSmallimageList.Images.Add(fileItem.Text, fileIcon); this.LVLargeimageList.Images.Add(fileItem.Text, fileIcon); fileItem.ImageKey = fileItem.Text; fileItem.Name = Driver.RootDirectory.FullName; if (Driver.DriveType == DriveType.Fixed) { fileItem.SubItems.Add("本地磁盘"); } else if (Driver.DriveType == DriveType.Removable) { fileItem.Text = "可移动磁盘(" Driver.Name.Remove(2) ")"; fileItem.SubItems.Add("可移动磁盘"); fileIcon = GetSystemIcon.GetIcon(Driver.Name, false); this.LVSmallimageList.Images.Add(fileItem.Text, fileIcon); this.LVLargeimageList.Images.Add(fileItem.Text, fileIcon); fileItem.ImageKey = fileItem.Text; } else if (Driver.DriveType == DriveType.CDRom) { fileItem.Text = "CDRom驱动器(" Driver.Name.Remove(2) ")"; fileItem.SubItems.Add("CDRom驱动器"); fileIcon = GetSystemIcon.GetIcon(Driver.Name, false); this.LVSmallimageList.Images.Add(fileItem.Text, fileIcon); this.LVLargeimageList.Images.Add(fileItem.Text, fileIcon); fileItem.ImageKey = fileItem.Text; } else { fileIcon = GetSystemIcon.GetIcon(Driver.Name, false); this.TVimageList.Images.Add(fileItem.Text, fileIcon); this.LVSmallimageList.Images.Add(fileItem.Text, fileIcon); fileItem.ImageKey = fileItem.Text; } if (Driver.IsReady) { double G = 1000000000.00D; //fileItem.SubItems.Add(Math.Round(Driver.TotalSize / G, 2) "G"); //fileItem.SubItems.Add(Math.Round(Driver.TotalFreeSpace / G, 2) "G"); fileItem.SubItems.Add(FileSize(Driver.TotalSize) ); fileItem.SubItems.Add(FileSize(Driver.TotalFreeSpace)); fileItem.Tag = Driver.TotalSize; } else if (Driver.DriveType == DriveType.CDRom) { //listView1.Items.Clear(); this.ExplorerTree.Focus(); } else { MessageBox.Show("驱动器未就绪,请检查设备!", "温柔提醒", MessageBoxButtons.OK); } } #endregion listView磁盘文件.EndUpdate(); FilePathStr = "我的电脑"; TxtAddress.Text = FilePathStr; } #endregion /// <summary> /// 为tn节点添加子节点 /// </summary> /// <param name="tn">TreeNode类型节点</param> #region private void AddDirectories(TreeNode tn) { tn.Nodes.Clear(); //MessageBox.Show(StrPath); DirectoryInfo dirInfo = new DirectoryInfo(tn.Tag.ToString()); if (!dirInfo.Exists) { return; } DirectoryInfo[] dirs; try { //获得节点下的所有文件夹 dirs = dirInfo.GetDirectories(); } catch (Exception) { //throw e; //MessageBox.Show(e.Message); return; } //添加子节点 foreach (DirectoryInfo dir in dirs) { if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } TreeNode sunTn = new TreeNode(dir.Name); sunTn.Tag = dir.FullName; tn.Nodes.Add(sunTn); //不能在这里用递归,速度太慢 //AddDirectories(SubTn); } } #endregion /// <summary> /// TreeView节点将要展开的前画出其子节点,同时更新ListView /// </summary> /// <param name="sender">Object类型系统参数</param> /// <param name="e">TreeViewCancelEventArgs类型系统参数</param> #region private void ExplorerTree_BeforeExpand(object sender, TreeViewCancelEventArgs e) { //返回当前计算机中逻辑驱动器名称的数组(已经在程序开始处生命) //string[] Drivers = Environment.GetLogicalDrives(); //这样重新设置所有的根节点不如只重新设置要展开的节点。 if (e.Node.FullPath == "我的电脑") { InitDisksList(); return; } TxtAddress.Text = e.Node.Tag.ToString(); //禁止任何树视图重绘 ExplorerTree.BeginUpdate(); foreach (TreeNode tn in e.Node.Nodes) { AddDirectories(tn); } ExplorerTree.EndUpdate(); //当打开某个节点的时候同时更新ListView1; ListUpdate(e.Node.Tag.ToString()); } #endregion /// <summary> /// 更新ListView(即画出选定目录下的目录和文件) /// </summary> /// <param name="newPath">绝对路径</param> #region private void ListUpdate(string newPath) { if (File.Exists(newPath)) { Process.Start(newPath); } else if (newPath == "我的电脑") { InitDisksList(); FilePathStr = newPath; } else if (Directory.Exists(newPath)) { #region//设置一般文件夹的ListView标题块 listView磁盘文件.Columns.Clear(); listView磁盘文件.Columns.Add("名称", 200, HorizontalAlignment.Left); listView磁盘文件.Columns.Add("修改时间", 115, HorizontalAlignment.Left); listView磁盘文件.Columns.Add("类型", 85, HorizontalAlignment.Left); listView磁盘文件.Columns.Add("大小", 80, HorizontalAlignment.Right); #endregion DirectoryInfo currentDir = new DirectoryInfo(newPath); DirectoryInfo[] dirs = new DirectoryInfo[0]; FileInfo[] files = new FileInfo[0]; dirs = currentDir.GetDirectories(); //获取目录 files = currentDir.GetFiles(); //获取文件 //删除ImageList中的程序图标 foreach (ListViewItem item in listView磁盘文件.Items) { if (item.Text.EndsWith(".exe")) //是程序 { LVLargeimageList.Images.RemoveByKey(item.Text); LVSmallimageList.Images.RemoveByKey(item.Text); } } // listView磁盘文件.BeginUpdate(); listView磁盘文件.Items.Clear(); if (listView磁盘文件.View == View.Details) { //添加上一层行 ListViewItem upItem = listView磁盘文件.Items.Add("上一层", "上一层", "上一层"); upItem.Name = "上一层"; upItem.SubItems.Add(""); upItem.SubItems.Add(""); upItem.SubItems.Add(""); } //列出文件夹 foreach (DirectoryInfo dir in dirs) { //如果是系统文件和隐藏文件,则不添加到列表 if ((dir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } ListViewItem dirItem = listView磁盘文件.Items.Add(dir.Name); //Icon fileIcon = GetSystemIcon.GetIcon(tn.Tag.ToString(), false); //this.TVimageList.Images.Add(tn.Text, fileIcon); //tn.ImageKey = tn.Text; Icon fileIcon = GetSystemIcon.GetIconByFileType(dir.Name, false); LVLargeimageList.Images.Add(dir.Name, fileIcon); LVSmallimageList.Images.Add(dir.Name, fileIcon); dirItem.ImageKey = dir.Name; dirItem.Name = dir.FullName; dirItem.SubItems.Add(dir.LastWriteTimeUtc.ToString("yyyy/MM/dd HH:mm")); dirItem.SubItems.Add("文件夹"); dirItem.SubItems.Add(""); } //列出文件 foreach (FileInfo file in files) { //如果是系统文件和隐藏文件,则不添加到列表 if ((file.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { continue; } ListViewItem fileItem = listView磁盘文件.Items.Add(file.Name); if (file.Extension == ".exe" || file.Extension == "") //程序文件或无扩展名 { Icon fileIcon = GetSystemIcon.GetIconByFileName(file.FullName); LVLargeimageList.Images.Add(file.Name, fileIcon); LVSmallimageList.Images.Add(file.Name, fileIcon); fileItem.ImageKey = file.Name; } else //其它文件 { if (!LVSmallimageList.Images.ContainsKey(file.Extension)) //ImageList中不存在此类图标 { Icon fileIcon = GetSystemIcon.GetIconByFileName(file.FullName); LVLargeimageList.Images.Add(file.Extension, fileIcon); LVSmallimageList.Images.Add(file.Extension, fileIcon); } fileItem.ImageKey = file.Extension; } fileItem.Name = file.FullName; fileItem.SubItems.Add(file.LastWriteTimeUtc.ToString("yyyy/MM/dd HH:mm")); string filetype = file.Extension.Equals("") ? "文件" : file.Extension.Substring(file.Extension.IndexOf('.') 1).ToUpper(); fileItem.SubItems.Add(filetype.Equals("文件") ? filetype : (filetype.Equals("DLL") ? "应用程序扩展" : filetype " 文件")); fileItem.SubItems.Add(FileSize(file.Length)); fileItem.Tag = file.Length; } listView磁盘文件.EndUpdate(); FilePathStr = newPath; TxtAddress.Text = newPath; //更新地址栏 lbinformation.Text = string.Format("文件夹{0}个,文件{1}个,共计{2}个对象", dirs.Length, files.Length, listView磁盘文件.Items.Count); //DoInformation.Text = "本地浏览器"; // "个对象"; //更新状态栏 } } #endregion public string FileSize(long size) { double newsize = double.Parse(size.ToString()); string returnStr = newsize > 1024 ? ((newsize/1024) > 1024 ? ((newsize / 1024 / 1024) > 1024 ? (((newsize / 1024 / 1024 / 1024).ToString("F") " G")) : ((newsize / 1024 / 1024).ToString("F") " M")) : ((newsize / 1024).ToString("F") " KB")) : (newsize " B"); return returnStr; } /// <summary> /// 选定某个节点后显示其下的文件夹和文件 /// </summary> /// <param name="sender">object类型系统参数</param> /// <param name="e">TreeViewEventArgs类型系统参数</param> #region private void ExplorerTree_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node.Text.Equals("我的电脑")) { InitDisksList(); } else { TxtAddress.Text = e.Node.Tag.ToString(); ListUpdate(TxtAddress.Text); } } #endregion #endregion #region 菜单按钮事件及方法 private void Btn向上Click(object sender, EventArgs e) { try { int index = FilePathStr.LastIndexOf("\\"); if (index > 2) { string newpath = FilePathStr.Substring(0, index); ListUpdate(newpath); } else { InitDisksList(); } } catch { } } //转到按钮事件 private void Btn转到Click(object sender, EventArgs e) { ListUpdate(TxtAddress.Text); } private void Btn刷新Click(object sender, EventArgs e) { CreatTreeview(); } //显示隐藏树目录按钮事件 private void Btn树目录Click(object sender, EventArgs e) { ExplorerTree.Visible = Btn目录树.Checked; } private void Btn选定Click(object sender, EventArgs e) { ListViewItem[] items = new ListViewItem[listView磁盘文件 .SelectedItems.Count]; for (int i = 0; i < items.Length; i ) { items[i] = listView磁盘文件.SelectedItems[i]; } AddFile(items); } //地址文本框相应键盘回车事件 private void AddressTxtKeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == '\r') { Btn转到Click(sender, null); } } //大图标显示按钮事件 private void 大图标ToolStripMenuItem_Click(object sender, EventArgs e) { //标题ToolStripMenuItem.CheckOnClick属性全部设为True; //即单击时选定,但是这个方法是在单击后执行所以不起作用 //还要手动设置选中 ClearCheckStatue(); listView磁盘文件.View = View.LargeIcon; ListUpdate(FilePathStr); 大图标ToolStripMenuItem.Checked = true; 大图标ToolStripMenuItem1.Checked = true; } //小图标显示按钮事件 private void 小图标ToolStripMenuItem_Click(object sender, EventArgs e) { ClearCheckStatue(); listView磁盘文件.View = View.SmallIcon; ListUpdate(FilePathStr); 小图标ToolStripMenuItem.Checked = true; 小图标ToolStripMenuItem1.Checked = true; } //列表显示按钮事件 private void 列表ToolStripMenuItem_Click(object sender, EventArgs e) { ClearCheckStatue(); listView磁盘文件.View = View.List; ListUpdate(FilePathStr); 列表ToolStripMenuItem.Checked = true; 列表ToolStripMenuItem1.Checked = true; } //详细信息显示按钮事件 private void 详细信息ToolStripMenuItem_Click(object sender, EventArgs e) { ClearCheckStatue(); listView磁盘文件.View = View.Details; ListUpdate(FilePathStr); 详细信息ToolStripMenuItem.Checked = true; 详细信息ToolStripMenuItem1.Checked = true; } //平铺信息显示按钮事件 private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e) { ClearCheckStatue(); listView磁盘文件.View = View.Tile; ListUpdate(FilePathStr); 标题ToolStripMenuItem.Checked = true; 标题ToolStripMenuItem1.Checked = true; listView磁盘文件.Refresh(); } //清理选定状态 private void ClearCheckStatue() { 大图标ToolStripMenuItem.Checked = false; 小图标ToolStripMenuItem.Checked = false; 详细信息ToolStripMenuItem.Checked = false; 列表ToolStripMenuItem.Checked = false; 标题ToolStripMenuItem.Checked = false; 大图标ToolStripMenuItem1.Checked = false; 小图标ToolStripMenuItem1.Checked = false; 详细信息ToolStripMenuItem1.Checked = false; 列表ToolStripMenuItem1.Checked = false; 标题ToolStripMenuItem1.Checked = false; } #endregion #region 右键菜单事件 /// <summary> /// 在指定目录新建一个文件 /// </summary> /// <param name="path">文件存放绝对路径</param> /// <param name="name">extension</param> /// <param name="extension">文件扩展名,类型</param> /// <returns></returns> public string CreateText(string path, string name, string extension) { int i = 0; String newpath = string.Format("{0}\\{1}{2}",path , name, extension); while (File.Exists(newpath)) { i ; newpath = string.Format("{0}\\{1}{2}{3}",path , name , i ,extension); } File.CreateText(newpath); //更新ListView1; ListUpdate(TxtAddress.Text); //设置可编辑标签模式 listView磁盘文件.LabelEdit = true; //编辑 newpath = newpath.Replace("\\\\", "\\"); listView磁盘文件.Items[newpath].BeginEdit(); return newpath; } /// <summary> /// 删除指定文件夹(或者文件) /// </summary> /// <param name="strFile">文件完整路径</param> public void DeleteDir(string strFile) { try { //是文件不是文件夹 if (File.Exists(strFile)) { File.Delete(strFile); } //是文件夹不是文件 else { DirectoryInfo dirInfo = new DirectoryInfo(strFile); //这里直接传参true表示删除其中子目录和文件夹; //dirInfo.Delete(true); //也可以自己递归实现 FileInfo[] files = dirInfo.GetFiles(); DirectoryInfo[] dirs = dirInfo.GetDirectories(); //此目录下有文件夹 foreach (FileInfo file in files) { DeleteDir(file.FullName); } //删除所有文件夹 foreach (DirectoryInfo dir in dirs) { DeleteDir(dir.FullName); } Directory.Delete(strFile); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e) { DialogResult tem = MessageBox.Show(string.Format(" 确定删除?"), string.Format("提醒"), MessageBoxButtons.OKCancel); if (tem == DialogResult.OK) { DeleteDir(listView磁盘文件.SelectedItems[0].Name); } ListUpdate(TxtAddress.Text); } private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { listView磁盘文件_DoubleClick(sender, e); } //复制文件或者文件夹 private void CopyFolder(string from, string to) { to = to.Replace("\\\\", "\\"); if (!Directory.Exists(to)) Directory.CreateDirectory(to); // 子文件夹 foreach (string sub in Directory.GetDirectories(from)) CopyFolder(sub "\\", to Path.GetFileName(sub) "\\"); // 文件 foreach (string file in Directory.GetFiles(from)) File.Copy(file, to Path.GetFileName(file), true); } //记录要复制的文件名 private string _temName = ""; private void 复制ToolStripMenuItem_Click(object sender, EventArgs e) { try { _temName = listView磁盘文件.SelectedItems[0].Text; CopyFolder(listView磁盘文件.SelectedItems[0].Name, "C:\\"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e) { try { CopyFolder("C:\\", TxtAddress.Text "\\" _temName "\\"); DeleteDir("C:\\"); ListUpdate(TxtAddress.Text); } catch (Exception ex) { MessageBox.Show(string.Format("文件读取错误" ex.Message)); } } private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e) { try { CopyFolder(listView磁盘文件.SelectedItems[0].Name, "C:\\"); DeleteDir(listView磁盘文件.SelectedItems[0].Name); } catch (Exception ex) { MessageBox.Show(ex.Message); } } //新建文件夹如果存在则文件名 private void 文件夹ToolStripMenuItem_Click(object sender, EventArgs e) { int i = 0; String newpath = string.Format("{0}\\新建文件夹", TxtAddress.Text); while (Directory.Exists(newpath)) { i ; newpath = string.Format("{0}\\新建文件夹{1}",TxtAddress.Text , i); } Directory.CreateDirectory(newpath); ListUpdate(TxtAddress.Text); listView磁盘文件.LabelEdit = true; newpath = newpath.Replace("\\\\", "\\"); listView磁盘文件.Items[newpath].BeginEdit(); } private void 文档txtToolStripMenuItem_Click(object sender, EventArgs e) { CreateText(TxtAddress.Text, "新建文件", ".txt"); } private void 文档docToolStripMenuItem_Click(object sender, EventArgs e) { CreateText(TxtAddress.Text, "新建文件", ".doc"); } private void 重MING名ToolStripMenuItem_Click(object sender, EventArgs e) { listView磁盘文件.LabelEdit = true; listView磁盘文件.SelectedItems[0].BeginEdit(); } private void 删除队列ToolStripMenuItem_Click(object sender, EventArgs e) { int deletecount = listView选中文件.SelectedItems.Count; int count = listView选中文件.Items.Count; foreach (ListViewItem item in listView选中文件.SelectedItems) { listView选中文件.Items.Remove(item); } DoInformation.Text = string.Format("已删除{0}个传输队列文件(剩余{1}个文件)", deletecount, listView选中文件.Items.Count); } private void 刷新ToolStripMenuItemClick(object sender, EventArgs e) { if (TxtAddress.Text.Trim().Equals("我的电脑")) { return; } ListUpdate(TxtAddress.Text.Trim()); } #endregion #region 其他事件 //双击打开磁盘文件文件 private void listView磁盘文件_DoubleClick(object sender, EventArgs e) { try { if(listView磁盘文件.SelectedItems.Count==0) return; string newpath = listView磁盘文件.SelectedItems[0].Name; if (newpath.Equals("上一层")) { Btn向上Click(null,null); } else if (Directory.Exists(newpath)) { ListUpdate(newpath); } else { Process.Start(newpath); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void listView磁盘文件_MouseUp(object sender, MouseEventArgs e) { GetInfomation(); } /// <summary> /// 更新选中的信息 /// </summary> public void GetInfomation() { try { long filesize = 0; int isup = 0; DirectoryInfo currentDir = new DirectoryInfo(FilePathStr); if (FilePathStr == "我的电脑") { for (int i = 0; i < listView磁盘文件.SelectedItems.Count; i ) { if (listView磁盘文件.SelectedItems[i].Tag != null) { filesize = (long)listView磁盘文件.SelectedItems[i].Tag; } } if (listView磁盘文件.SelectedItems.Count == 0) lbinformation.Text = string.Format("共{0}个对象", listView磁盘文件.Items.Count); else lbinformation.Text = string.Format("共计{0}个对象 (已选中{1}个对象,共{2})", listView磁盘文件.Items.Count, listView磁盘文件.SelectedItems.Count, FileSize(filesize)); return; } DirectoryInfo[] dirs = currentDir.GetDirectories(); //获取目录 FileInfo[] files = currentDir.GetFiles(); //获取文件 if (listView磁盘文件.SelectedItems.Count > 0) { for (int i = 0; i < listView磁盘文件.SelectedItems.Count; i ) { if (listView磁盘文件.SelectedItems[i].Name.Equals("上一层")) { isup = 1; continue; } if (listView磁盘文件.SelectedItems[i].Tag != null) { filesize = (long) listView磁盘文件.SelectedItems[i].Tag; } } if ((listView磁盘文件.SelectedItems.Count - isup) == 0) lbinformation.Text = string.Format("文件夹{0}个,文件{1}个,共计{2}个对象", dirs.Length, files.Length, listView磁盘文件.Items.Count); else lbinformation.Text = string.Format("文件夹{0}个,文件{1}个,共计{2}个对象 (已选中{3}个对象,共{4})", dirs.Length, files.Length, listView磁盘文件.Items.Count, listView磁盘文件.SelectedItems.Count - isup, FileSize(filesize)); } else { lbinformation.Text = string.Format("文件夹{0}个,文件{1}个,共计{2}个对象", dirs.Length, files.Length, listView磁盘文件.Items.Count); } } catch(Exception ex) { lbinformation.Text = string.Format(ex.Message); } DoInformation.Text = listView选中文件.Items.Count == 0 ? "本地浏览器" : string.Format("(传输队列中共{0}个文件)",listView选中文件.Items.Count); } private void listView磁盘文件_SelectedIndexChanged(object sender, EventArgs e) { GetInfomation(); } //右键弹出菜单栏 private void listView选中文件_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { contextMenuStrip2.Show(listView选中文件, new Point(e.X, e.Y)); } } //Delete键删除队列和全选选中文件 private void listView选中文件KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Delete) { 删除队列ToolStripMenuItem_Click(null, null); } if(e.Control&&e.KeyCode==Keys.A) { for (int i = 0; i < listView选中文件.Items.Count; i ) { listView选中文件.Items[i].Selected=true; } } } //磁盘文件全选 private void listView磁盘文件KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.A) { for (int i = 0; i < listView磁盘文件.Items.Count; i ) { listView磁盘文件.Items[i].Selected = true; } } } //激活控件时触发事件 private void EnterEven(object sender, EventArgs e) { StatePanel.BackColor = Color.YellowGreen; if (listView选中文件.Items.Count > 0) { Btn传输.Enabled = true; } else { Btn传输.Enabled = false; } } //失去焦点时出发事件 private void LeaveEven(object sender, EventArgs e) { StatePanel.BackColor = Color.Silver; if (listView选中文件.Items.Count > 0) { Btn传输.Enabled = true; } else { Btn传输.Enabled = false; } } #endregion #region 拖拽文件 private void listView磁盘文件_ItemDrag(object sender, ItemDragEventArgs e) { //如果是鼠标右键则返回 if (e.Button == MouseButtons.Right) { return; } //判断当前控件里是否有item,如果没有则返回 if (listView磁盘文件.SelectedIndices.Count <= 0) { return; } ListViewItem[] itemTo = new ListViewItem[((ListView)sender).SelectedItems.Count]; for (int i = 0; i < itemTo.Length; i ) { itemTo[i] = ((ListView)sender).SelectedItems[i]; } ((ListView)(sender)).DoDragDrop(itemTo, DragDropEffects.Copy); } //远程文件列表有item拖进时事件 private void listView选中文件_DragEnter(object sender, DragEventArgs e) { //判断是否目前拖动的数据是ListViewItem,如果是,则拖动符串对目的组件进行拷贝 if (e.Data.GetDataPresent("System.Windows.Forms.ListViewItem[]")) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } //远程文件列表的DragDrop事件 private void listView选中文件_DragDrop(object sender, DragEventArgs e) { ListViewItem[] items = (ListViewItem[])(e.Data.GetData("System.Windows.Forms.ListViewItem[]")); AddFile(items); } //添加文件 private void AddFile(ListViewItem[] items) { StringBuilder strBuilder = new StringBuilder(); int DropCount = 0; foreach (ListViewItem item in items) { ListViewItem temp = new ListViewItem(item.SubItems[0].Text, item.ImageIndex); temp.ImageKey = item.ImageKey; temp.Tag = item; temp.Text = item.SubItems[0].Text; temp.SubItems.Add(item.SubItems[0].Name); temp.SubItems.Add(SitPath); temp.SubItems.Add(FileSize(item.Tag == null ? 0 : (long) item.Tag)); temp.SubItems.Add(string.Format("上传至服务器:{0}", ServerName)); if (item.Tag != null && !IsInlistView选中文件(item.SubItems[0].Name)&&FilePathStr!="我的电脑") { DropCount ; temp.ForeColor = Color.DarkSalmon; //temp.ForeColor = Color.Green; strBuilder.Remove(0, strBuilder.Length); strBuilder.Append(Path.GetExtension(temp.SubItems[0].Text)); listView选中文件.Items.Add(temp); } } listView选中文件.Refresh(); DoInformation.Text = (items.Length - DropCount) > 0 ? string.Format("成功选定{0}个传输文件,{1}个文件已存在传输队列中(传输队列中共{2}个文件)", DropCount, items.Length - DropCount, listView选中文件.Items.Count) : string.Format("成功选定{0}个传输文件(传输队列中共{1}个文件)", DropCount, listView选中文件.Items.Count); if (listView磁盘文件.SelectedItems.Count>0||FilePathStr == "我的电脑") { DoInformation.Text = "磁盘不可当做文件拖动到传输队列中"; return; } } /// <summary> /// 判断listView选中文件中是否已有该项 /// </summary> /// <returns></returns> public bool IsInlistView选中文件(string fullpath) { bool ishave = false; foreach (ListViewItem temp in listView选中文件.Items) { if (temp.SubItems[1].Text.Equals(fullpath)) { ishave = true; } } return ishave; } #endregion}}
评论